//
//  HR_Touch_HomeViewController.m
//  Pods
//
//  Created by duanhuifen on 2017/7/24.
//
//

#import "HR_Touch_HomeViewController.h"
#import "HR_Touch_TestCell.h"
#import "HR_Touch_PreviewViewController.h"

@interface HR_Touch_HomeViewController ()<
UITableViewDelegate,
UITableViewDataSource,
UIViewControllerPreviewingDelegate
>
@property (strong, nonatomic) UITableView *myTableView;

@end

@implementation HR_Touch_HomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTableView = myTableView;
    [self.view addSubview:myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.title = @"3DTouch 首页";
    [self sendNoti];
    
    //    [self.myTableView registerNib:[UINib nibWithNibName:@"HR_Touch_TableViewCell" bundle:nil] forCellReuseIdentifier:touchCellId];
    
    //    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
    //        [self registerForPreviewingWithDelegate:self sourceView:self.myTableView];
    //        NSLog(@"3D Touch 可用!");
    //    } else {
    //        NSLog(@"3D Touch 无效");
    //    }
    [self registerForPreviewingWithDelegate:self sourceView:self.myTableView];  //注册代理
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    HR_Touch_TableViewCell * touchCell = [tableView dequeueReusableCellWithIdentifier:touchCellId];
    //    // 判断 3D Touch 是否可用
    //     return touchCell;
    HR_Touch_TestCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(!cell)
    {
        cell=[[HR_Touch_TestCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    
    
    NSIndexPath * IndexPath = [self.myTableView indexPathForRowAtPoint:location];
    HR_Touch_TestCell * cell = [self.myTableView cellForRowAtIndexPath:IndexPath];  //获取touch的那个cell
    
    NSInteger index=3;
    
    NSMutableArray * array =[NSMutableArray array];
    [array addObject:cell.headerImageView];
    [array addObject:cell.zanBtn];
    [array addObject:cell.commentBtn];
    
    for(UIView * view in array)
    {
        
        if([self touchedView:view and:location])
        {
            
            if([view isKindOfClass:[UIButton class]]) //判断touch是不是在button上
            {
                UIButton*btn = (UIButton*)view;
                
                index=[array indexOfObject:btn]; // index为1或者2
            }
            else if([view isKindOfClass:[UIImageView class]])  //判断touch是不是在imageview上
            {
                index=0;
            }
            
        }
        
    }
    
    /*
     只有 设置了 previewingContext.sourceRect，你touch的那个控件才会突出显示，其他区域变模糊。
     */
    
    
    HR_Touch_PreviewViewController * previewVC=[[HR_Touch_PreviewViewController alloc]init];
    
    previewVC.customControllerName = @"HR_Touch_CustomController";
    //    previewVC.preViewActionArr = @[@"喜欢",@"点赞",@"分享"];
    previewVC.previewType = HR_Touch_PreviewType_Custom;
    CGRect rect;
    if(index==0){
        rect = [self getFrameWithView:cell.headerImageView fromView:cell toView:self.view andWhetherContainNav:YES];
        previewVC.previewType = HR_Touch_PreviewType_Image;
        previewVC.imageName = @"细长";
        //        previewVC.preferredContentSize = CGSizeMake(40, 40);
    }else if(index==1){
        rect = [self getFrameWithView:cell.zanBtn fromView:cell toView:self.view andWhetherContainNav:YES];
    }else if(index==2){
        rect = [self getFrameWithView:cell.commentBtn fromView:cell toView:self.view andWhetherContainNav:YES];
    }else{
        rect =  cell.frame;
    }
    previewingContext.sourceRect = rect;
    
    
    return previewVC;
    
}

-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self showViewController:viewControllerToCommit sender:nil];
}

-(BOOL)touchedView:(UIView*)view and:(CGPoint)location
{
    CGPoint point = [view convertPoint:location fromView:self.myTableView];
    
    return CGRectContainsPoint(view.bounds,point);
}

//根据导航算frame
- (CGRect)getFrameWithView:(UIView *)view fromView:(UIView *)fromView toView:(UIView *)toView andWhetherContainNav:(BOOL)isContainNav{
    CGRect rect = [toView convertRect:view.frame fromView:fromView];
    if (isContainNav) {
        CGRect newRect = rect;
        newRect.origin.y -= 64;
        return newRect;
    }else{
        return rect;
    }
}

- (void)sendNoti{
    // 1.创建本地通知
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    // 2.设置本地通知的信息
    localNote.alertBody = @"吃饭了吗?";
    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
    localNote.alertAction = @"快点";
    localNote.hasAction = NO;
    localNote.alertTitle = @"3DTouch 通知";
    localNote.applicationIconBadgeNumber = 10;
    // 3.调度通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}

@end
